package com.guguskins.back.service.impl;

import com.guguskins.back.common.security.entity.GuUserSrcurity;
import com.guguskins.back.entity.GuAdmin;
import com.guguskins.back.entity.GuAdminUser;
import com.guguskins.back.service.GuAdminService;
import com.guguskins.back.service.GuAdminUserService;
import com.guguskins.back.service.GuPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

@Service(value = "UserDetailsService")
public class UserDetailsServiceImpl implements UserDetailsService {
    @Autowired
   private GuAdminUserService  guAdminUserService;
    @Autowired
    private GuPermissionService guPermissionService;
    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {

        GuAdminUser user = guAdminUserService.findByName(s);
        //判断
        if(user == null) {
            throw new UsernameNotFoundException("用户不存在");
        }

        //根据用户查询用户权限列表
        List<String> permissionValueList = guPermissionService.selectPermissionValueByUserId(user.getId());
      GuUserSrcurity securityUser = new GuUserSrcurity(user);
        securityUser.setAdminUser(user);
        securityUser.setJurisdictionList(permissionValueList);
        return securityUser;

    }
    }


